<?php
declare (strict_types = 1);

namespace app\admin\controller;

use think\App;
use app\common\builder\TableBuilder;
use app\common\builder\TableFront;
use think\facade\View;
use think\response\Json;

class ProCate extends Base
{

    //主键
    protected $pk = 'id';

    //主模型
    protected $model = '\app\common\model\ProCate';
    protected $promodel = '\app\common\model\Pro';

    public function index($id)
    {
        //print_r($id);die;
        return $this->setTable($id);
    }

    //自动构建table,按需修改
    public function setTable($id)
    {
        $tableFront = TableFront::getInstance()
            ->addRow(['type' => 'checkbox', 'fixed' => 'left', 'field' => '', 'title' => '全选'])
            ->addRow(['field' => 'id', 'sort' => true, 'title' => 'ID', 'width' => 80])
            ->addRow(['field' => 'title', 'title' => '标题', 'modify' => 'add|edit', 'format' => 'input'])
            ->addRow(['field' => 'score', 'title' => '分数', 'modify' => 'add|edit', 'format' => 'number'])
            ->addRow(['field' => 'sort', 'title' => '排序', 'width' => 120, 'modify' => 'add|edit', 'format' => 'input',
            // 'templet' => '<div>
            // <input type="text" data-key="sort" data-id="{{d.id}}" data-url="' . url('update') . '" value="{{d.sort}}" class="layui-input x-sort tableInput"/>
            // </div>'
        ])
            ->addRow(['field' => 'create_time', 'title' => '创建时间'])
            ->addRow(['field' => 'update_time', 'title' => '更新时间'])
            ->addRow(['fixed' => 'right', 'align' => 'center', 'toolbar' => '#barDemo', 'title' => '操作', 'width' => 200]);

        return TableBuilder::getInstance()
            ->addColumns($tableFront)
            ->addRightButtons(['edit' => ['width' => 1000, 'height' => 1000], 'delete'])
            ->addTopButtons([
            'add' => [
                'type' => 'add',
                'title' => '新增',
                'width' => 1000,
                'height' => 1000,
                'href' => url('procate/add', ['name' => request()->controller() . '_' . request()->action(),'id'=>$id])->build(),
            ], 'deleteAll'])
            ->setSearch($this->setSearch())
            ->addExtraMap(['pro_id'=>$id])
            ->fetch();
    }
    //自动构建Search，按需修改
    public function setSearch()
    {
        return TableFront::getInstance()
            ->setSearch(['title', '名称', 'input', '', '', '', 'like']);
    }

    public function add($name = '', $tpl = 'procate/add')
    {
        $params = $this->request->param();
        //print_r($params);die;
        //$procate=$this->model::where(array('pro_id'=>$params['id'],'is_del'=>0))->find();
        $score=$this->model::where(array('pro_id'=>$params['id'],'is_del'=>0))->sum('score');
        $promodel=$this->promodel::where(['id'=>$params['id'],'is_del'=>0])->find();
        View::assign('pro_id', $params['id']);
        View::assign('sort', ($this->model::where(array('pro_id'=>$params['id'],'is_del'=>0))->max('sort')+1));
        View::assign('score', (($promodel['num']-$score)>0?($promodel['num']-$score):0));
        return parent::add($name, $tpl); // TODO: Change the autogenerated stub
    }

    public function save($param = [], $model = true): Json
    {
        return parent::save($param, $model); // TODO: Change the autogenerated stub
    }


    public function getCate()
    {
        $id = $this->request->param('id', 0);
        return json($this->model::getCate($id));
    }
    
}
